package com.kedacom.emptyproject.map.behavior

import android.content.Context
import android.support.design.widget.CoordinatorLayout
import android.util.AttributeSet
import android.view.View
import com.kedacom.emptyproject.map.widget.MyConstraintLayoutBehaviorView

/**
 * @author GuanHaoran
 * @description:
 * @date :2020/10/19 10:52 AM
 */
class LocationBehavior<V : View>(
    context: Context,
    attrs: AttributeSet?
) : CoordinatorLayout.Behavior<V>(context, attrs) {

    constructor(ctx: Context) : this(context = ctx, attrs = null)


    override fun layoutDependsOn(parent: CoordinatorLayout, child: V, dependency: View): Boolean {
//        return dependency.id == R.id.clBeahvior
        onDependentViewChanged(parent, child, dependency)
        return dependency is MyConstraintLayoutBehaviorView
    }

    override fun onDependentViewChanged(
        parent: CoordinatorLayout,
        child: V,
        dependency: View
    ): Boolean {
        if (dependency.visibility == View.GONE) {
            child.y = (parent.height - parent.height / 6f) - child.height * 1.5f
            child.alpha = 1f
            return true
        }


        if (dependency.top <= (parent.height - parent.height / 6)) {
            child.y = dependency.top.toFloat() - child.height * 1.5f
        } else {
            child.y = (parent.height - parent.height / 6f) - child.height * 1.5f
        }
        if (dependency.top < parent.height / 2) {
            child.alpha = 1 - ((parent.height / 2f - dependency.top) / 100f) * 0.2f
        }

//        Log.e("qin"," child.alpha = { child.alpha}")
        return true
    }
}